<!-- $Id: _form.php,v 1.1 2011-06-17 04:43:20 herve Exp $ -->

<div class="yiiForm">

<?php $this->widget('application.components.RequiredMessage'); ?>

<?php echo CHtml::beginForm(); ?>

<?php echo CHtml::errorSummary($model); ?>

<?php
$opts = array (
	'size' => 50,
	'maxlength' => 64
);
if ($update)
$opts['readonly'] = '1';
?>

	<div class="simple">
	<?php echo CHtml::activeLabelEx($model,'name'); ?>
	<?php echo CHtml::activeTextField($model,'name',$opts); ?>
	</div>

	<div class="simple">
	<?php echo CHtml::activeLabelEx($model,'description'); ?>
	<?php echo CHtml::activeTextField($model,'description',array('size'=>50,'maxlength'=>64)); ?>
	</div>

	<div class="simple">
	<?php echo CHtml::activeLabelEx($model,'operations'); ?>
		<div>
			<ul style="border: 0px; padding-top: 0px; margin-top: 0px"
				id="operations">
				<?php
				$tasks = Yii::app()->authManager->getTasks();
				$style = 'width:auto;float:none;text-align:left;display:inline;margin:0px 0px 0px 5px;';
				$allTasks = array();

				foreach ($tasks as $tmp)
				{
					$arr = split("\.", $tmp->name);
					if (! isset ($allTasks[$arr[0]]))
					{
						$allTasks[$arr[0]] = array();
					}
					$allTasks[$arr[0]][] = $tmp;
				}

				foreach ($allTasks as $r => $tasks)
				{
					echo "<li>$r<ul>";

					foreach ($tasks as $task)
					{
						$opts1 = array();
						$opts2 = array();
						if (isset($model->operations[$task->name]))
						{
							$opts1['checked'] = 'checked';
							$opts1['value'] = $task->name;
							$opts2['disabled'] = 'disabled';
							$opts2['readonly'] = 'readonly';
						}

						echo "<li>" .
						CHtml::activeCheckBox($model, 'operations['.$task->name.']', $opts1) .
						CHtml::activeLabelEx($model, 'operations['.$task->name.']', array('label' => $task->description, 'style' => $style));
						$operations = $task->getChildren();

						if (isset($operations) && count($operations) > 0)
						{
							echo "\n<ul>";
							foreach ($operations as $operation)
							{
								if (isset($model->operations[$operation->name]))
								{
									$opts2['checked'] = 'checked';
								}else{
									unset($opts2['checked']);
								}
								$opts2['value'] = $operation->name;
								echo "<li>" .
								CHtml::activeCheckBox($model, 'operations['.$operation->name.']', $opts2) .
								CHtml::activeLabelEx($model, 'operations['.$operation->name.']', array('label' => $operation->description, 'style' => $style)) .
				"</li>";
							}
							echo "</ul>";
						}
						echo "</li>\n";
					}
					echo "</ul></li>";
				}

				?></ul>
		</div>
	</div>

	<div class="action">
	<?php echo CHtml::submitButton($update ? Yii::t('views','Save') : Yii::t('views','Create')); ?>
	</div>

	<?php echo CHtml::endForm(); ?>

</div>
<!-- yiiForm -->

	<?php
	$script =<<<JS
$('#operations').find('input[type="checkbox"]').bind('change', function(){
	var children = $(this).parent().children('ul').find('input[type="checkbox"]');
	if ($(this).is(':checked'))
	{
		// check all sub tasks
		children.attr("checked", true).attr("readonly", true).attr("disabled", true);
	}else{
		children.attr("readonly", false).attr("disabled", false);
	}
});
JS;

$cs = Yii::app()->clientScript;
$cs->registerScript('c_topics_form_save', $script);
$cs->registerScriptFile(Yii::app()->baseUrl . '/js/topic_objective.js', CClientScript::POS_HEAD);

?>